package com.shycloud.mido.music.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.shycloud.common.minio.service.MinioTemplate;
import com.shycloud.mido.common.core.constant.CommonConstants;
import com.shycloud.mido.common.core.util.R;
import com.shycloud.mido.music.domain.MisMusicLibrary;
import com.shycloud.mido.music.dto.MisMusicLibraryDTO;
import com.shycloud.mido.music.dto.MusicLibrarySearchDto;
import com.shycloud.mido.music.mapper.MisMusicLibraryMapper;
import com.shycloud.mido.music.service.MisMusicCategoryService;
import com.shycloud.mido.music.service.MisMusicLibraryService;
import com.shycloud.mido.music.util.CommonString;
import com.shycloud.mido.music.vo.CourseWareMusicIPageVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * 曲库模块 Controller
 *
 * @author nianhua.jiang
 * @date 2020/4/27
 */
@RestController
@AllArgsConstructor
@RequestMapping("/musicLibrary")
@Api(tags = "APP谱库大全接口")
public class MusicLibraryController {

	/** 曲库模块 Service 接口 */
	private MisMusicLibraryService musicLibraryService;
	/** 分类模块 Service 接口 */
	private MisMusicCategoryService misMusicCategoryService;
	/** 曲库模块 mapper */
	private MisMusicLibraryMapper libMapper;

	/** minio 交互类 */
	private final MinioTemplate minioTemplate;

	/***/
	private RedisTemplate redisTemplate;

	/**
	 * 分页查询曲库
	 *
	 * @param misMusicLibraryDTO 【添加/编辑】曲库 DTO
	 * @return com.shycloud.mido.common.core.util.R
	 * @author nianhua.jiang
	 * @date 2020/4/29 15:45
	 */
	@GetMapping("/getMusicLibrary")
	@ApiOperation("分页查询曲库")
	@Cacheable(value = "musicLibrary", key = "'musicLibrary'+#misMusicLibraryDTO.cateId.get(0)+'-'+#misMusicLibraryDTO.getCurrent()")
	public R getMusicLibrary(MisMusicLibraryDTO misMusicLibraryDTO) {
		return new R(musicLibraryService.getMusicLibrary(misMusicLibraryDTO));
	}

	/**
	 * 分页查询曲库
	 *
	 * @param 【添加/编辑】曲库 DTO
	 * @return com.shycloud.mido.common.core.util.R
	 * @author nianhua.jiang
	 * @date 2020/4/29 15:45
	 */
	@GetMapping("/getMusicLibraryMini")
	@ApiOperation("分页查询曲库")
	@Cacheable(value = "musicLibraryMini", key = "'musicLibraryMini'+#cateId+'-'+#current")
	public R getMusicLibraryMini(Integer current, Integer size, Integer cateId) {
		return new R(musicLibraryService.getMusicLibraryMini(new Page(current, size), cateId));
	}

	/**
	 * 查询所属分类的名称
	 *
	 * @param
	 * @return com.shycloud.mido.common.core.util.R
	 * @author nianhua.jiang
	 * @date 2020/4/29 15:45
	 */
	@GetMapping("/getCateName")
	@ApiOperation("查询所属分类的名称")
	public R getCateName() {
		return new R(misMusicCategoryService.getCateNameList());
	}

	/**
	 * 【添加/编辑】曲库
	 *
	 * @param misMusicLibraryDto
	 * @return com.shycloud.mido.common.core.util.R
	 * @author nianhua.jiang
	 * @date 2020/4/29 17:02
	 */
	@PostMapping("saveMusicLibrary")
	@ApiOperation("【添加/编辑】曲库")
	public R saveMusicLibrary(@RequestBody MisMusicLibraryDTO misMusicLibraryDto) {

		Set musicLibrary = redisTemplate.keys("musicLibrary*");
		if (!musicLibrary.isEmpty()) {
			redisTemplate.delete(musicLibrary);
		}
		if (musicLibraryService.saveMusicLibrary(misMusicLibraryDto)) {
			return new R();
		} else {
			return new R().setCode(1).setMsg("教程名称重复,请重新输入");
		}
	}

	/**
	 * 上传封面图片
	 *
	 * @param img 图片URL
	 * @return com.shycloud.mido.common.core.util.R
	 * @author nianhua.jiang
	 * @date 2020/4/30 9:26
	 */
	@PostMapping("uploadImage")
	@ApiOperation("上传封面图片")
	public R uploadImage(@RequestParam("img") MultipartFile img) {

		//提取图片名称
		String imgName = img.getOriginalFilename().replace(" ", "");
		//初始化map结果集
		Map<String, String> resultMap = new HashMap<>(4);
		//名称
		resultMap.put("name", imgName);
		try {
			//上传操作
			resultMap.put("url", minioTemplate.uploadFile(img.getInputStream(), imgName));
		} catch (Exception e) {
			return R.builder().code(CommonConstants.FAIL).msg(e.getLocalizedMessage()).build();
		}
		return R.builder().data(resultMap).build();
	}

	/**
	 * 删除曲库
	 *
	 * @param id
	 * @return com.shycloud.mido.common.core.util.R
	 * @author nianhua.jiang
	 * @date 2020/4/30 16:05
	 */
	@GetMapping("delMusicLibrary")
	@ApiOperation("删除曲库")
	public R delMusicLibrary(Integer id) {
		Set musicLibrary = redisTemplate.keys("musicLibrary*");
		if (!musicLibrary.isEmpty()) {
			redisTemplate.delete(musicLibrary);
		}
		//根据ID检索曲库表
		MisMusicLibrary misMusicLibrary = musicLibraryService.getById(id);
		//删除
		misMusicLibrary.setDelFlag(CommonString.DELETE_FLG_DEL);
		//返回结果
		return new R(musicLibraryService.updateById(misMusicLibrary));

	}

	//下方是后台的接口
	@GetMapping("/list")
	@ApiOperation("查询曲库列表")
	public R list() {
		QueryWrapper queryWrapper = new QueryWrapper<>();

		//查询 del_flag = 0的数据
		queryWrapper.eq("del_flag", CommonConstants.STATUS_NORMAL);

		return new R(libMapper.selectList(queryWrapper));
	}

	@GetMapping("/page")
	@ApiOperation("分页查询曲库")
	public R page(MusicLibrarySearchDto search) {
		return new R(libMapper.page(search));
	}

	@PostMapping("/update")
	@ApiOperation("更新曲库")
	public R update(@RequestBody MisMusicLibrary library) {

		//==2020/08/28 nianhua.jiang ADD START ======================//
		Set musicLibrary = redisTemplate.keys("musicLibrary*");
		if (!musicLibrary.isEmpty()) {
			redisTemplate.delete(musicLibrary);
		}
		//==2020/08/28 nianhua.jiang ADD E N D ======================//
		return new R(libMapper.updateById(library));
	}

	@GetMapping("getLibraryAndCollList")
	@ApiOperation(value = "获取曲库和曲集列表", tags = "课件整合相关接口")
	@ApiImplicitParam(name = "cateId", value = "分类ID", paramType = "query", dataType = "int", required = true)
	public R<IPage<CourseWareMusicIPageVO>> getLibraryAndCollList(Integer current, Integer size,
			Integer cateId) {
		return new R(musicLibraryService.getLibraryAndCollList(new Page(current, size), cateId));
	}

}